<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_gupage_ide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


// ------------------------------------------------------------------------

/**
 * 单页
 *
 * @category Admin
 */

class PageAction extends AdminAction
{

	protected $Page;


	public function _initialize()
	{
		parent::_initialize();
		$this->Page = D('Page');
	}


	//单页列表
	public function pageList()
	{
		import('ORG.Util.Page');

		$total = $this->Page->where($where)->count();
		$Page = new Page($total);
		$pagination = $Page->show();
		$pageList = $this->Page->limit($Page->firstRow.','.$Page->listRows)->select();

		$this->assign('pagination', $pagination);
		$this->assign('pageList', $pageList);
		$this->display();
	}


	//新增单页
	public function addPage()
	{
		$this->editPage();
	}


	//编辑单页
	public function editPage()
	{
		$pageID = isset($_REQUEST['page_id']) ? intval($_REQUEST['page_id']) : 0;
		$pageInfo = $this->Page->getPageByID($pageID);

		if ($this->isPost())
		{
		    $_validate = array(
        		array('title', 'require', '{%page.page_title_required}'),
        	);
            $this->Page->setProperty("_validate", $_validate);
			if (!$this->Page->create())
			{
				$this->error($this->Page->getError());
			}

			$data = array(
				'title'     => $_POST['title'],
				'content'   => $_POST['content'],
				'edit_time' => time(),
			);

			if (empty($pageInfo))
			{
			    $data['create_time'] = time();
				$this->Page->add($data);
				$message = L('page.page_add_succeed');
				if (!empty($_POST['link_to_nav']))
				{
                    $data = array(
                        'name'     => $_POST['nav_name'],
                        'url'      => $_POST['nav_url'],
                        'target'   => $_POST['nav_target'],
                        'position' => $_POST['nav_position'],
                        'status'   => $_POST['nav_status'],
                    );
                    $Navigation = M('Navigation');
                    $Navigation->add($data);
				}
			}
			else
			{
				$where = array('page_id' => $pageID);
				$this->Page->where($where)->save($data);
			}
			$message = L('page.page_edited_succeed');
			$this->assign('jumpUrl', __URL__.'/pageList');
		    $this->success($message);
		}
		else
		{
		    $navStatusRadios = array(1 => L('yes'), 0 => L('no'));
			$this->assign('navStatusRadios', $navStatusRadios);
			$this->assign('navStatusChecked', 1);

		    $navTargetRadios = array(
				'_blank' => L('_blank'),
				'_self'  => L('_self'),
				'_top'   => L('_top'),
			);
			$this->assign('navTargetRadios', $navTargetRadios);
			$this->assign('navTargetChecked', '_blank');

			$navPositionRadios = array(
				'top'    => L('nav.top'),
				'middle' => L('nav.middle'),
				'bottom' => L('nav.bottom'),
			);
			$this->assign('navPositionRadios', $navPositionRadios);
			$this->assign('navPositionChecked', 'top');

			$this->assign('rs', $pageInfo);

			$this->display('editPage');
		}
	}


	//管理单页
	public function managePage()
	{
		if ($this->isPost())
		{
			if (empty($_POST['mods']))
			{
				$this->redirect('Admin-Page/pageList');
			}
			if (isset($_POST['action']['delete']))
			{
			    $this->Page->deletePageByID($_POST['mods']);
			    $this->success(L('page.page_deleted_succeed'));
			}
	    }
	}

}


/* End of file MiscAction.class.php */
/* Location: ./App/Lib/Action/Admin/MiscAction.class.php */